﻿/*-----------------------------------------------
// File: EntranceState.cs 
// Description: 渲染层入场状态
// Author: Shaobing	492057342@qq.com
-----------------------------------------------*/
using UnityEngine;
using System.Collections;

namespace Battle.View
{
    public class EntranceState : Fsm.FsmStateMonoBase<BattleState> 
    {
        BattleViewCtrl viewCtrl = null;
        public EntranceState(BattleState state, BattleViewCtrl ctrl) : base(state, ctrl)
        {
            viewCtrl = ctrl;
        }

        public const float attackerEntranceTime = 1.5f;
        float deltaTime = 0f;
        public override void Enter()
        {
            DefenderEntrance();
            AttackerEntranceInit();
            deltaTime = 0f;
            isAttackerEntranced = false;

            AudioManager.PlaySceneMusic(StaticData.curBattleData.mapSceneName);

            CameraController.Instance.Entrance();
            if (StaticData.curBattleData.battleType == BattleType.PVE)
            {
                ManagerController.Instance.CopyBattleLoadComplete();
                //LoadingPanel.Instance.HideMask(1);
            }
            else
            {
                LoadingPanel.Instance.HideLoading();
            }

        }
        void DefenderEntrance() {
            foreach (var uid in viewCtrl.GetActorUids(BattleCamp.Defender)) {
                var bc = viewCtrl.GetBattleCharacter(uid);
                if (bc.isAlive)
                {
                    bc.SwitchState(ActorState.Entrance);
                }
            }
        }
        void AttackerEntranceInit() {
            foreach (var uid in viewCtrl.GetActorUids(BattleCamp.Attacker)) {
                var bc = viewCtrl.GetBattleCharacter(uid);
                bc.transform.localPosition = new Vector3(10000, 10000, 10000);
            }
        }
        void AttackerEntrance() {
            foreach (var uid in viewCtrl.GetActorUids(BattleCamp.Attacker)) {
                var bc = viewCtrl.GetBattleCharacter(uid);
                if (bc.isAlive)
                {
                    bc.SwitchState(ActorState.Entrance);
                }
            }
            isAttackerEntranced = true;
        }

        public override void Leave()
        {
            CameraController.Instance.Ready();
            BattleManager.Instance.InitBattleUI();

            // 战斗的自动、速度处理
            if (StaticData.curBattleData.battleType == BattleType.PVE||
                StaticData.curBattleData.battleType == BattleType.FIELD_PVE || 
                StaticData.curBattleData.battleType == BattleType.FIELD_BOSS)
            {
                if (viewCtrl.isAuto)
                {
                    BattleManager.Instance.SetSpeed(StaticData.pveSpeedCache);
                    BattleManager.Instance.battlePanel.speedButton.GetComponent<SpeedBtn>().Refresh();
                }
            }
            else if (StaticData.curBattleData.battleType == BattleType.PVP
                || StaticData.curBattleData.battleType == BattleType.FIELD_PVP)
            {
                BattleManager.Instance.SetSpeed(StaticData.pvpSpeedCache);
                BattleManager.Instance.battlePanel.speedButton.GetComponent<SpeedBtn>().Refresh();
            }
            else if (StaticData.curBattleData.battleType == BattleType.PLUNDER)
            {
                BattleManager.Instance.SetSpeed(StaticData.pluderSpeedCache);
                BattleManager.Instance.battlePanel.speedButton.GetComponent<SpeedBtn>().Refresh();
            }
            
        }

        bool isAttackerEntranced = false;
        public override void Update()
        {
            deltaTime += Time.deltaTime;
            if (deltaTime >= attackerEntranceTime && !isAttackerEntranced) {
                AttackerEntrance();
            }

            if (isEntranceOver()) {
                viewCtrl.SwitchState(BattleState.BattleRunningState);
            }
        }
        bool isEntranceOver() {
            foreach (var uid in viewCtrl.GetActorUids(BattleCamp.Attacker)) {
                var bc = viewCtrl.GetBattleCharacter(uid);
                if (bc != null && bc.isAlive) {
                    var state = bc.GetCurrentState() as BCEntranceState;
                    if (state != null)
                    {
                        if (!state.isEntranceOver)
                        {
                            return false;
                        }
                    }
                    else {
                        return false;
                    }
                }
            }
            foreach (var uid in viewCtrl.GetActorUids(BattleCamp.Defender)) {
                var bc = viewCtrl.GetBattleCharacter(uid);
                if (bc != null && bc.isAlive)
                {
                    var state = bc.GetCurrentState() as BCEntranceState;
                    if (state != null)
                    {
                        if (!state.isEntranceOver)
                        {
                            return false;
                        }
                    }
                    else {
                        return false;
                    }
                }
            }
            return true;
        }
    }

}